gsettings set org.gnome.system.proxy.http host '127.0.0.1'
gsettings set org.gnome.system.proxy.http port 7890
gsettings set org.gnome.system.proxy.socks host '127.0.0.1'
gsettings set org.gnome.system.proxy.socks port 7890
gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '::1']"
# gsettings set org.gnome.system.proxy mode 'none'
gsettings set org.gnome.system.proxy mode 'manual'
export http_proxy="http://127.0.0.1:7890"
export https_proxy="http://127.0.0.1:7890"


gsettings set org.gnome.settings-daemon.plugins.media-keys home "['<Super>e']"
# 不显示桌面主目录
gsettings set org.gnome.shell.extensions.ding show-home false
gsettings set org.gnome.shell.extensions.ding start-corner 'top-left'

# 显示时钟秒数
gsettings set org.gnome.desktop.interface clock-show-seconds true

# .bash_history
sed '/HISTSIZE/s/\<1000\>/-1/' -i ~/.bashrc
sed '/HISTFILESIZE/s/\<2000\>/-1/' -i ~/.bashrc

# 命令历史记录立刻追加到 ~/.bash_history 文件中, 会导致zellij无法记录新的数据
sed -i '$a export PROMPT_COMMAND="history -a"' ~/.bashrc

# 禁用wayland, 启用x11
sudo sed '/WaylandEnable/s/^#//' /etc/gdm3/custom.conf -i 
sudo sed '$aQT_QPA_PLATFORM=xcb' /etc/environment -i


export LANG=en_US
xdg-user-dirs-gtk-update
export LANG=zh_CN


if [ -z `which bat` ]; then  
    sudo apt install -y build-essential
    sudo apt install -y xclip net-tools arp-scan dconf-editor lm-sensors
    sudo apt install -y wget dos2unix neofetch bat curl ripgrep git gitk git-lfs bear fd-find jq exa duf
    sudo apt install -y xclip net-tools libfuse2 inotify-tools nfs-common openssh-server openssh-client
    sudo apt install -y qalc qalculate-gtk geogebra 
    sudo apt install -y qemu-kvm virt-manager libvirt-daemon-system virtinst libvirt-clients bridge-utils
    sudo systemctl enable --now libvirtd
    sudo systemctl start libvirtd
    sudo usermod -aG kvm $USER
    sudo usermod -aG libvirt $USER
    sudo usermod -aG dialout $USER
    sudo usermod -aG wireshark $USER

    sudo ln -s $(which fdfind) /usr/bin/fd
    sudo service ssh start
    sudo systemctl status nfs-server
fi


if [ -z `which microsoft-edge-stable` ]; then  
# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EB3E94ADBE1229CF
	curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
	sudo install -o root -g root -m 644 microsoft.gpg /etc/apt/trusted.gpg.d/
	sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
	## Install
	sudo apt update
	sudo apt install microsoft-edge-stable
	sudo rm microsoft.gpg
fi

if [ -z `which rustc` ]; then 
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    rustup update

    sudo apt install -y cargo
    echo '[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = "ustc" 
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
    ' > ~/.cargo/config

    # atuin
    cargo install atuin
    # nvim ~/.config/atuin/config.toml
    # 1. enter编辑而不是直接运行`enter_accpet = false`
    # 2. 禁用up按键 `eval "$(atuin init bash --disable-up-arrow)"`
    # 3. 配置要在fzf后

    curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
    echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
    echo 'eval "$(atuin init bash --disable-up-arrow)"' >> ~/.bashrc
    source ~/.bashrc

    cargo install eza du-dust tree-sitter-cli
    cargo install xcp
    # cargo install git-delta
    # cargo install --locked bottom
    # cargo install teehee
    # cargo install --locked tealdeer
    # cargo install --locked watchexec-cli

    cargo install --locked zellij
    mkdir ~/.config/zellij
    zellij setup --dump-config > ~/.config/zellij/config.kdl

    # cargo install --locked starship
    # sed -i '$a eval "$(starship init bash)"' ~/.bashrc

    cargo install --locked zoxide
    sed -i '$a eval "$(zoxide init bash)"' ~/.bashrc
    cargo install --locked yazi-fm yazi-cli

    cargo install --locked navi
    sed -i '$a eval "$(navi widget bash)"' ~/.bashrc

    cargo install onefetch
    # 查看csv, excel文件
    cargo install tabiew   
fi

# wget --timeout=3 --tries=3 -P ~/Downloads https://github.com/muesli/duf/releases/download/v0.8.1/duf_0.8.1_linux_amd64.deb
# sudo dpkg -i ~/Downloads/duf_0.8.1_linux_amd64.deb

if [ -z `which guake` ]; then  
    sudo apt install -y guake 
    dconf write /apps/guake/keybindings/global/show-hide "'<Alt>w'"
    dconf write /apps/guake/keybindings/local/switch-tab1 "'<Alt>1'"
    dconf write /apps/guake/keybindings/local/switch-tab2 "'<Alt>2'"
    dconf write /apps/guake/keybindings/local/switch-tab3 "'<Alt>3'"
    dconf write /apps/guake/keybindings/local/switch-tab4 "'<Alt>4'"
    dconf write /apps/guake/keybindings/local/switch-tab5 "'<Alt>5'"
    # dconf write /apps/guake/keybindings/local/split-tab-vertical "'<Alt>Right'"
    # dconf write /apps/guake/keybindings/local/split-tab-horizontal "'<Alt>Down'"

    dconf write /apps/guake/style/font "'FiraMono Nerd Font 14'"
    dconf write /apps/guake/style/background/transparency 100m
    dconf write /apps/guake/general/start-at-login true
    dconf write /apps/guake/general/window-height 70
    dconf write /apps/guake/general/window-width 100
    dconf write /apps/guake/general/window-losefocus true
    dconf write /apps/guake/general/tab-close-buttons true
fi

if [ ! -d "$HOME/.config/autostart" ]; then
    mkdir -p $HOME/.config/autostart
fi
if [ -z `which ulauncher` ]; then  
    # plugin
    # https://github.com/hillaryychan/ulauncher-fzf
    #
    sudo add-apt-repository -y ppa:agornostal/ulauncher && sudo apt update && sudo apt install -y ulauncher
    mkdir $HOME/.config/ulauncher
    echo "[Desktop Entry]
Type=Application
Exec=ulauncher
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Comment=Application launcher for Linux
Name=ulauncher " > ~/.config/autostart/ulauncher.desktop

    echo '{
    "blacklisted-desktop-dirs": "/usr/share/locale:/usr/share/app-install:/usr/share/kservices5:/usr/share/fk5:/usr/share/kservicetypes5:/usr/share/applications/screensavers:/usr/share/kde4:/usr/share/mimelnk",
    "clear-previous-query": true,
    "disable-desktop-filters": false,
    "grab-mouse-pointer": false,
    "hotkey-show-app": "<Alt>q",
    "render-on-screen": "mouse-pointer-monitor",
    "show-indicator-icon": true,
    "show-recent-apps": "0",
    "terminal-command": "",
    "theme-name": "light"
} ' > ~/.config/ulauncher/settings.json
fi

if [ -z `which node` ]; then 
    wget -P ~/Downloads https://nodejs.org/dist/v24.9.0/node-v24.9.0-linux-x64.tar.xz
    tar -vxf ~/Downloads/node-v24.9.0-linux-x64.tar.xz -C ~/Downloads
    sudo mv ~/Downloads/node-v24.9.0-linux-x64 /opt/node

    # /etc/profile后追加以下环境变量
    sudo sed '$a NODE_HOME=/opt/node' -i /etc/profile
    sudo sed '$a PATH=$PATH:$NODE_HOME/bin' -i /etc/profile
    sudo sed '$a NODE_PATH=$NODE_HOME/lib/node_mudules' -i /etc/profile
    source /etc/profile
    
    sudo ln -s /opt/node/bin/node /usr/local/bin/node
    sudo ln -s /opt/node/bin/npm /usr/local/bin/npm
    
    npm config set registry http://registry.npm.taobao.org/
    if [ -z `which yarn` ]; then
        curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
        echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
        sudo apt update
        sudo apt install -y yarn
    fi
    yarn config set registry https://registry.npm.taobao.org -g
    yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g
fi

if [ -z `which lua` ]; then 
    sudo apt install -y lua5.1 luarocks
    sudo apt install -y sqlite3 libsqlite3-dev sqlitebrowser
    sudo luarocks install luv
    sudo luarocks install sqlite3
fi

if [ -z `which nvim` ]; then  
    sudo add-apt-repository -y ppa:neovim-ppa/unstable
    sudo apt update
    sudo apt install -y neovim

    #wget --timeout=3 --tries=3 -e "https_proxy=http://127.0.0.1:7890" -P ~/Downloads https://github.com/neovim/neovim/releases/download/v0.10.1/nvim-linux64.tar.gz
    #tar xvf ~/Downloads/nvim-linux64.tar.gz -C ~/Downloads
    #sudo cp -r ~/Downloads/nvim-linux64/* /usr/local/

    yarn global add neovim
    sudo npm install -g neovim
    sudo npm install -g tree-sitter

    git clone --filter=blob:none --single-branch git@github.com:folke/lazy.nvim.git ~/.local/share/nvim/lazy
    [ ! -d ~/.vim/dict/ ] && mkdir -p ~/.vim/dict/
    cp ~/SynologyDrive/Trans.json ~/.vim/dict/
    wget --timeout=3 --tries=3 -e "https_proxy=http://127.0.0.1:7890" -P ~/Downloads https://github.com/skywind3000/ECDICT/releases/download/1.0.28/ecdict-sqlite-28.zip
    unzip ~/Downloads/ecdict-sqlite-28.zip -d ~/.vim/dict
    mv ~/.vim/dict/stardict.db ~/.vim/dict/ultimate.db
fi

source sub00_fcitx5.sh


if [ -z `which code` ]; then  
    sudo sh -c "echo 'deb [arch=amd64] http://packages.microsoft.com/repos/vscode stable main' > /etc/apt/sources.list.d/vscode.list"
    curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
    sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EB3E94ADBE1229CF
    sudo apt update
    sudo apt install -y code
fi


if [ -z `which wps` ]; then  
    wget -P ~/Downloads https://wps-linux-personal.wpscdn.cn/wps/download/ep/Linux2019/11711/wps-office_11.1.0.11711_amd64.deb
    sudo dpkg -i ~/Downloads/wps-office_*.deb
    sudo cp ~/vimrc/linux_config/wps/* /usr/share/fonts
    sudo mkfontscale
    sudo mkfontdir
    sudo fc-cache
    # ln -s ~/vimrc/.config/nvim ~/.config/nvim
fi

wget --timeout=3 --tries=3 -P ~/Downloads https://github.com/itchyny/bed/releases/download/v0.2.5/bed_v0.2.5_linux_amd64.tar.gz
tar xvf ~/Downloads/bed_v0.2.5_linux_amd64.tar.gz -C ~/Downloads
sudo cp ~/Downloads/bed_v0.2.5_linux_amd64/bed /usr/bin/
